<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\Comment;
use app\api\model\Friend;
use app\api\service\Oss;
use think\facade\Cache;
use think\Request;

class Circle
{

    public function upload(Request $request){
        $path = $request->file('file')->getPathname();
        $arr = Oss::upload($path);
        return json($arr);
    }


    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
       $uid =$request->uid;
       $data = Friend::where('uid1',$uid)->column('uid2');
       $data[] = $uid;
       $arr= \app\api\model\Circle::with('user')->whereIn('userid',$data)->paginate(3)->toArray();
       foreach ($arr['data'] as $key => $v){
         $comments =  Comment::with('user')->where('circleid',$v['id'])->select()->toArray();
           $arr['data'][$key]['comments'] = $comments;
       }
       return json(['code'=>200,'data'=>$arr['data'],'current_page'=>$arr['current_page'],'last_page'=>$arr['last_page']]);
    }

    public function comment(Request $request){
        $uid =$request->uid;
        $params = $request->param();
        $params['userid'] = $uid;
       $flag =  Comment::create($params);
       if ($flag){
           return json(['code'=>200,'msg'=>'评论成功']);
       }else{
           return json(['code'=>300,'msg'=>'评论失败']);
       }
    }

    public function getComment(){

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
       $parmas = $request->param();
       $add_time = time();
       $parmas['add_time'] = $add_time;
       $parmas['userid'] = $request->uid;
       $flag = \app\api\model\Circle::create($parmas);
       if ($flag){
           return  json(['code'=>200,'msg'=>'发布成功']);
       }else{
           return  json(['code'=>300,'msg'=>'发布失败']);
       }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
